package myservice.android.activities;

import myservice.android.Global;
import myservice.android.R;
import myservice.android.database.Database;
import myservice.android.numberpicker.NumberPicker;
import myservice.android.numberpicker.NumberPicker.OnChangedListener;
import myservice.android.utilities.EditTextMonitor;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MinistryDetailsActivity extends BaseSaveableActivity {
	
	private NumberPicker ministryHoursPicker;
	private NumberPicker ministryMinutesPicker;
	private NumberPicker creditHoursPicker;
	private NumberPicker creditMinutesPicker;
	private NumberPicker magazinesPicker;
	private NumberPicker brochuresPicker;
	private NumberPicker booksPicker;
	private NumberPicker returnCallsPicker;	
	private LinearLayout creditTime;
	private EditText commentsText;
	private Button saveButton;
	
	private int date,month,year;
	private boolean newRecord;
	
	@Override
	protected void initializeLayoutIdAndTitle() {
		layoutId = R.layout.ministry_details;
		activityTitle = Global.res.getString(R.string.title_ministry_details);
	}

	@Override
	protected void retrieveViews() {
        ministryHoursPicker = (NumberPicker) findViewById(R.id.ministryHoursPicker);
        ministryMinutesPicker = (NumberPicker) findViewById(R.id.ministryMinutesPicker);
        creditHoursPicker = (NumberPicker) findViewById(R.id.creditHoursPicker);
        creditMinutesPicker = (NumberPicker) findViewById(R.id.creditMinutesPicker); 
        creditTime = (LinearLayout) findViewById(R.id.creditTime);  
        magazinesPicker = (NumberPicker) findViewById(R.id.magazinesPicker);
        brochuresPicker = (NumberPicker) findViewById(R.id.brochuresPicker);
        booksPicker = (NumberPicker) findViewById(R.id.booksPicker);
        returnCallsPicker = (NumberPicker) findViewById(R.id.returnCallsPicker); 
        creditTime = (LinearLayout) findViewById(R.id.creditTime);
        commentsText = (EditText) findViewById(R.id.commentsText);   
        saveButton = (Button) findViewById(R.id.saveButton);
        
        ministryHoursPicker.setRange(0, 999);
        ministryMinutesPicker.setIncrement(5);
        ministryMinutesPicker.setRange(0, 55);        
        creditHoursPicker.setRange(0, 999);
        creditMinutesPicker.setIncrement(5);
        creditMinutesPicker.setRange(0, 55);           
        magazinesPicker.setRange(0, 999);        
        brochuresPicker.setRange(0, 999);                
        booksPicker.setRange(0, 999);                
        returnCallsPicker.setRange(0, 999);                               
	}

	@Override
	protected void loadActivityParameters() {
		date = activityParameters.getInt("date");
		month = activityParameters.getInt("month");
		year = activityParameters.getInt("year");
	}

	@Override
	protected void assignListeners() {
		
		OnChangedListener numberPickerListener = new OnChangedListener() {
			@Override
			public void onChanged(NumberPicker picker, int oldVal, int newVal) {
				activityContentModified = true;
			}			
		};		
		ministryHoursPicker.setOnChangeListener(numberPickerListener);
        ministryMinutesPicker.setOnChangeListener(numberPickerListener);        
        creditHoursPicker.setOnChangeListener(numberPickerListener);
        creditMinutesPicker.setOnChangeListener(numberPickerListener);           
        magazinesPicker.setOnChangeListener(numberPickerListener);       
        brochuresPicker.setOnChangeListener(numberPickerListener);                
        booksPicker.setOnChangeListener(numberPickerListener);              
        returnCallsPicker.setOnChangeListener(numberPickerListener);  
        
        commentsText.addTextChangedListener(new EditTextMonitor(this));
		
        saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				save();
				finish();
			}
        });
	}

	@Override
	protected void updateUi() {
		activityTitleView.setText(activityTitle+" ("+Global.getFormattedDate(year,month,date)+")");
		
    	if (Global.trackCreditTime==true)
    		creditTime.setVisibility(View.VISIBLE);
    	else
    		creditTime.setVisibility(View.GONE);	
    	
    	Cursor cursor = Database.select("SELECT * FROM ministry WHERE date="+date+" AND month="+month+" AND year="+year);
    	
    	if (cursor.moveToNext()==true) {
    		newRecord=false;
    		ministryHoursPicker.setCurrent(cursor.getInt(cursor.getColumnIndex("hours")));
    		ministryMinutesPicker.setCurrent(cursor.getInt(cursor.getColumnIndex("minutes")));
    		creditHoursPicker.setCurrent(cursor.getInt(cursor.getColumnIndex("chours")));   
    		creditMinutesPicker.setCurrent(cursor.getInt(cursor.getColumnIndex("cminutes")));    		
    		magazinesPicker.setCurrent(cursor.getInt(cursor.getColumnIndex("magazines")));
    		brochuresPicker.setCurrent(cursor.getInt(cursor.getColumnIndex("brochures")));
    		booksPicker.setCurrent(cursor.getInt(cursor.getColumnIndex("books")));
    		returnCallsPicker.setCurrent(cursor.getInt(cursor.getColumnIndex("returncalls")));
    		commentsText.setText(cursor.getString(cursor.getColumnIndex("comments")));    		
    	} else
    		newRecord=true;
    	
    	cursor.close();      	
	}    
    
    @Override
    protected void save() {   
    	
    	ministryHoursPicker.clearFocus();
    	ministryMinutesPicker.clearFocus();
    	creditHoursPicker.clearFocus();
    	creditMinutesPicker.clearFocus();
    	magazinesPicker.clearFocus();
    	brochuresPicker.clearFocus();
    	booksPicker.clearFocus();
    	returnCallsPicker.clearFocus();
    	
		if (newRecord==true) {
			Database.execSQL("INSERT INTO ministry (date,month,year,hours,minutes,chours,cminutes,magazines,brochures,books,returncalls,comments,timestamp) "+
		                            "VALUES ("+date+","+month+","+year+","+ministryHoursPicker.getCurrent()+","+ministryMinutesPicker.getCurrent()+
		                            ","+creditHoursPicker.getCurrent()+","+creditMinutesPicker.getCurrent()+","+magazinesPicker.getCurrent()+","+brochuresPicker.getCurrent()+
		                            ","+booksPicker.getCurrent()+","+returnCallsPicker.getCurrent()+",'"+Global.fixText(commentsText.getText())+"',"+Global.getTimeStamp()+")");
		} else {
			Database.execSQL("UPDATE ministry SET hours="+ministryHoursPicker.getCurrent()+", minutes="+ministryMinutesPicker.getCurrent()+", "+
									"chours="+creditHoursPicker.getCurrent()+", cminutes="+creditMinutesPicker.getCurrent()+", magazines="+magazinesPicker.getCurrent()+", "+
					                "brochures="+brochuresPicker.getCurrent()+", books="+booksPicker.getCurrent()+", returncalls="+returnCallsPicker.getCurrent()+", "+
									"comments='"+Global.fixText(commentsText.getText())+"', timestamp="+Global.getTimeStamp()+" WHERE date="+date+" AND month="+month+" AND year="+year);
		}		  
		
    }

}
